//
//  GoodsManager.m
//  shoppingMall
//
//  Created by Bear on 2016/10/28.
//  Copyright © 2016年 dragontrail. All rights reserved.
//

#import "GoodsManager.h"
#import "GoodsModel.h"

@implementation GoodsManager

+(instancetype)defaultManager{

    static GoodsManager* manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[GoodsManager alloc] init];
    });
    return manager;
}


-(NSArray *)GoodsStoreArr{

    return [self.GoodsTotalDic allKeys];
}

-(BOOL)isExistStoreWithStoreName:(NSString*)storeName{

    NSArray* storeNameArr = [self.GoodsSelectedDic allKeys];
    for (NSString* storeName_exist in storeNameArr) {
        if ([storeName isEqualToString:storeName_exist]) {
            return YES;
        }
    }
    return NO;

}

-(void)takeGoodWithGoods:(GoodsModel *)model IsSeleted:(BOOL)select{

    model.isSelected = select;
    if (select) {

        [self addGoodsToSelectedDicWithGood:model];
    }else{
        [self takeOutGoodsFromSelectedDicWithGood:model];
    }
}

-(void)addGoodsToSelectedDicWithGood:(GoodsModel*)model{

    NSMutableArray* storeArr = nil;
    if ([self isExistStoreWithStoreName:model.goodsStore]) {
        storeArr = [[self.GoodsSelectedDic objectForKey:model.goodsStore] mutableCopy];
        [storeArr addObject:model];

    }else{
        storeArr = [NSMutableArray arrayWithObject:model];
    };
    [self.GoodsSelectedDic setObject:storeArr forKey:model.goodsStore];
    [self countTotalPrice];
}

-(void)takeOutGoodsFromSelectedDicWithGood:(GoodsModel*)model{

    if (![self isExistStoreWithStoreName:model.goodsStore]) {
        return;
    }
    NSMutableArray* storeArr = [[self.GoodsSelectedDic objectForKey:model.goodsStore] mutableCopy];
    [storeArr removeObject:model];
    if (storeArr.count == 0) {
        [self.GoodsSelectedDic removeObjectForKey:model.goodsStore];
    }else{
        [self.GoodsSelectedDic setObject:storeArr forKey:model.goodsStore];
    }
    [self countTotalPrice];
}

-(void)createGoodsList{
    NSArray* imageName = @[@"test_1.jpg",@"test_2.jpg"];
    NSArray* goodsName = @[@"海儿",@"无人区",@"解放了电脑",@"在哪，那里是解放了",@"科大老鹅",@"下来的咖啡机",@"泄露等几方面",@"克雷德姆斯",@"思考满分",@"拉拉队服"];
    NSMutableArray* goodsArr_huaZhongStore = [NSMutableArray array];
    for (int i = 0; i<10; i++) {
        GoodsModel* goods = [[GoodsModel alloc]init];
        goods.goodsName = goodsName[i];
        goods.imgUrl = imageName[i%2];
        goods.price_now = @((i*arc4random()%1000)+1/1.00);
        goods.goodID =[NSString stringWithFormat:@"%d",i+100 ];
        goods.goodsStore = @"华中商店";
        goods.num = @(i);
        goods.goodsInfo = @"颜色：红 分类：无";
        [goodsArr_huaZhongStore addObject:goods];
    }
    NSMutableArray* goodsArr_huaDongStore = [NSMutableArray array];
    for (int i = 0; i<8; i++) {
        GoodsModel* goods = [[GoodsModel alloc]init];
        goods.goodsName = goodsName[i];
        goods.imgUrl = imageName[i%2];
        goods.price_now = @((i*arc4random()%1000)+1/1.00);
        goods.goodID =[NSString stringWithFormat:@"%d",i+100 ];
        goods.goodsStore = @"华东商店";
        goods.num = @(i);
        goods.goodsInfo = @"颜色：红 分类：无";
        [goodsArr_huaDongStore addObject:goods];
    }

    NSDictionary* dictionory = @{@"华东商店":goodsArr_huaDongStore,@"华中商店":goodsArr_huaZhongStore};
    self.GoodsTotalDic = [dictionory mutableCopy];
}

-(float)countTotalPrice{

    float totalprice = 0.0f;
    for (NSArray* array in self.GoodsTotalDic.allValues) {
        for (GoodsModel* model in array) {
            if (model.isSelected) {
                totalprice = totalprice + [model.price_now floatValue]*[model.num
                                                                         intValue];
            }
        }
    }
    self.goodsTotalPrice = totalprice;
    return totalprice;

}

-(NSMutableDictionary *)GoodsSelectedDic{

    if (!_GoodsSelectedDic) {
        _GoodsSelectedDic = [NSMutableDictionary dictionary];
    }
    return _GoodsSelectedDic;
}

@end
